/*
 * Copyright (C) 2009-2018 Hangzhou 2Dfire Technology Co., Ltd. All rights reserved
 */
package com.ws.soa.consumer.controller;

import com.google.common.base.Preconditions;
import com.ws.soa.consumer.domain.AccountDO;
import com.ws.soa.consumer.dto.AccountLoginDTO;
import com.ws.soa.consumer.dto.AccountRegisterDTO;
import com.ws.soa.consumer.service.AccountService;
import com.ws.soa.consumer.service.SessionStorageClientService;
import com.ws.soa.consumer.vo.LoginVO;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * UserController
 *
 * @author luobosi@2dfire.com
 * @since 2018-11-19
 */
@RestController
@RequestMapping(("/account"))
public class UserController {

  @Resource
  private SessionStorageClientService sessionStorageClientService;
  @Resource
  private AccountService accountService;
  @RequestMapping("/index")
  public String sayHello() {
    return "Hello,World!";
  }

  @PostMapping(value = "/request_param")
  public String requestParam(@RequestParam(value = "name") String name) {
    return name;
  }

  /**
   * 用户注册
   *
   * @return true/false
   */
  @PostMapping(value = "/v1/register")
  public String register(@RequestBody AccountRegisterDTO registerDTO) {

    Preconditions.checkArgument(null != registerDTO);
    String mobile = registerDTO.getMobile();
    Preconditions.checkArgument(StringUtils.isNotEmpty(mobile));
    AccountDO account = this.accountService.findAccountByMobile(mobile);

    if (null != account) {
      return "该手机号已经存在";
    }
    this.accountService.save(registerDTO, true);
    return "注册成功";
  }

  /**
   * 用户登录
   *
   * @return true/false
   */
  @PostMapping(value = "/v1/login")
  public LoginVO login(@RequestBody AccountLoginDTO accountLoginDTO) {
    AccountDO accountDO = this.accountService.findAccountByMobile(accountLoginDTO.getMobile());
    if (null == accountDO) {
      return LoginVO.builder().errorMessage("该用户不存在，请先注册").build();
    }

    if (!accountDO.getPassword().equals(accountLoginDTO.getPassword())) {
      return LoginVO.builder().errorMessage("密码不正确，请重新输入密码").build();
    }
    return LoginVO.builder().customerId(accountDO.getId()).token(accountDO.getId()).build();
  }

  /**
   * 修改用户密码
   *
   * @param oldPassword 旧密码
   * @param newPassword 新密码
   * @return true/false
   */
  @PostMapping(value = "/v1/password/modify")
  public Boolean updateAccountPassword(HttpServletRequest request,
                                       @RequestParam(value = "old_password") String oldPassword,
                                       @RequestParam(value = "new_password") String newPassword) {
    return Boolean.TRUE;
  }


}
